#!/usr/bin/env python

## info on gdata library can be found here
##    https://developers.google.com/gdata/articles/python_client_lib
##
import gdata.spreadsheet  
import gdata.spreadsheet.service
import socket
import urllib2
from xml.dom.minidom import parseString
import time
import httplib

### NOTE:  somebody needs to log into the xantrex gateway through a browser at least once
###           before running this.

# google spreadsheet settings
client = gdata.spreadsheet.service.SpreadsheetsService()  
client.email = 'abogussaddress@gmail.com'  # the google e-mail address hosting the google (web) site
client.password = 'abogusspassword'  # password for the google account above (what sucks is that this will be in the clear in this source)
client.ProgrammaticLogin() 


# file IO setup

current_date_time = time.strftime('%Y%m%d_%H%M_')
print current_date_time

system_datalog_filename = current_date_time + "powersystem_datalog.txt"
system_datalog_fd = open(system_datalog_filename, "w", 0)

xw6048_datalog_filename = current_date_time + "xw6048_datalog.txt"
xw6048_datalog_fd = open(xw6048_datalog_filename, "w", 0)

mppt60_0_datalog_filename = current_date_time + "mppt60_0_datalog.txt"
mppt60_0_datalog_fd = open(mppt60_0_datalog_filename, "w", 0)

mppt60_1_datalog_filename = current_date_time + "mppt60_1_datalog.txt"
mppt60_1_datalog_fd = open(mppt60_1_datalog_filename, "w", 0)

xwags_datalog_filename = current_date_time + "xwags_datalog.txt"
xwags_datalog_fd = open(xwags_datalog_filename, "w", 0)


def powersystem_data(system_datalog_fd):
    # file = urllib2.urlopen('http://192.168.1.153/xml/status.xml')
    while True:
        try:
            file = urllib2.urlopen('http://192.168.1.153/xml/status.xml')
            break
        except urllib2.HTTPError:
            time.sleep(10)    
    data = file.read()
    print data
    file.close()
    dom = parseString(data)
    xmlTag = dom.getElementsByTagName('system')[0].toxml()
    #print xmlTag

    str_data = xmlTag.encode("ascii")

    split_data = str_data.split()
    #print split_data

    new_list = []
    for value in split_data:
        new_value = value.strip('<>/')
        new_list.append(new_value)

    #print new_list

    data_list = []
    for another_value in new_list:
        a_new_value = another_value.split("=")
    #    print a_new_value
        if len(a_new_value) == 2:
            data_list.append(a_new_value)

    #print data_list

    data_dictionary = {}

    data_dictionary['date'] = time.strftime('%m/%d/%Y')
    data_dictionary['time'] = time.strftime("%H:%M")                           

    for data_value in data_list:
        if (str(data_value[0]) == "battery"):
            data_dictionary['battery'] = data_value[1].strip('"')
        if (str(data_value[0])== "acinv"):
            data_dictionary['acinv'] = data_value[1].strip('"')
        if (str(data_value[0]) == "dcbatt"):
            data_dictionary['dcbatt'] = data_value[1].strip('"')
        if (str(data_value[0])== "dcinv"):
            data_dictionary['dcinv'] = data_value[1].strip('"')
        if (str(data_value[0])== "lifetime"):
            data_dictionary['lifetime'] = data_value[1].strip('"')
        if (str(data_value[0])== "pv"):
            data_dictionary['pv'] = data_value[1].strip('"')

    if data_dictionary.has_key('battery') == False:
        data_dictionary['battery'] = "none"
    if data_dictionary.has_key('acinv') == False:
        data_dictionary['acinv'] = "none"
    if data_dictionary.has_key('dcbatt') == False:
        data_dictionary['dcbatt'] = "none"
    if data_dictionary.has_key('dcinv') == False:
        data_dictionary['dcinv'] = "none"
    if data_dictionary.has_key('lifetime') == False:
        data_dictionary['lifetime'] = "none"                                                                                                                    
    if data_dictionary.has_key('pv') == False:
        data_dictionary['pv'] = "none"    

    print data_dictionary['date'] + " " + data_dictionary['time'] + " " + data_dictionary['battery'] + " " + data_dictionary['acinv'] + " " + data_dictionary['dcbatt'] + " " + data_dictionary['dcinv'] + " " +  data_dictionary['lifetime'] + " " + data_dictionary['pv']

    data_sample = data_dictionary['date'] + " " + data_dictionary['time'] + " " + data_dictionary['battery'] + " " + data_dictionary['acinv'] + " " + data_dictionary['dcbatt'] + " " + data_dictionary['dcinv'] + " " +  data_dictionary['lifetime'] + " " + data_dictionary['pv'] + "\r\n"

    system_datalog_fd.write(data_sample)

    # write to google spreadsheet "power_system"
    while True:
        try:
            entry = client.InsertRow(data_dictionary, key='hcvEUABGELASTBOGUSSKEYHER4234544234334214123')  # get this key from the google spreadsheet's url
                                                                       # everything after key= and before &hl=en
            break
        except socket.error:
            time.sleep(10)
        except gdata.service.RequestError:
            time.sleep(60)
            client.ProgrammaticLogin()

    if isinstance(entry, gdata.spreadsheet.SpreadsheetsList):
      print "Insert row succeeded."
    else:
      print "Insert row failed."    

    return 0
    ## end of def powersystem_data(system_datalog_fd)


def xw6048_data(xw6048_datalog_fd):
    # file = urllib2.urlopen('http://192.168.1.153/xml/XW6048%200/rb.xml')
    while True:
        try:
            file = urllib2.urlopen('http://192.168.1.153/xml/XW6048%200/rb.xml')
            break
        except urllib2.HTTPError:
            time.sleep(10)    
    ### IF AT THIS POINT YOU A "httplib.BadStatusLine: HTTP/1.0 200Document follows"
    ### EXCEPTION GETS THROWN, YOU WILL HAVE TO CHANGE THE FOLLOWING IN THE 
    ### c:\Python27\lib\httplib.py CODE AFTER LINE 377 OF THE _read_status()
    ### FUNCTION DEFINITION.  INSERT THE FOLLOWING TWO LINES AFTER LINE 377
    ### OF THAT httplib.py CODE:
    ###
    ###    if status == "200Document":
    ###       status = "200"
    ###
    ### REASON WHY:  WHOEVER WROTE THE WEBSERVER FOR THIS SYSTEM MADE A TYPO
    ###    THEY DID NOT HAVE A SPACE BETWEEN 200 AND DOCUMENT FOR THE STATUS
    ###    RESPONSE TO THE HTTP GET REQUEST.
    
    data = file.read()
    file.close()
    print data
    dom = parseString(data)
    DcBatteryxmlTag = dom.getElementsByTagName('group')[0].toxml()
    Ac1AC1xmlTag = dom.getElementsByTagName('group')[1].toxml()
    AcInput2AC2xmlTag = dom.getElementsByTagName('group')[2].toxml()
    AcOutput2CriticalxmlTag = dom.getElementsByTagName('group')[3].toxml()

    #print DcBatteryxmlTag
    #print Ac1AC1xmlTag
    #print AcInput2AC2xmlTag
    #print AcOutput2CriticalxmlTag

    # just putting them into a single list that I can iterate through
    data_list = []
    data_list.append(DcBatteryxmlTag.encode("ascii"))
    data_list.append(Ac1AC1xmlTag.encode("ascii"))
    data_list.append(AcInput2AC2xmlTag.encode("ascii"))
    data_list.append(AcOutput2CriticalxmlTag.encode("ascii"))

    data_dictionary = {}

    data_dictionary['date'] = time.strftime('%m/%d/%Y')
    data_dictionary['time'] = time.strftime("%H:%M")  

    key_list = []

    for data_line in data_list:
        split_data = data_line.split()
        #print data_line
        new_list = []
        for value in split_data:
            new_value = value.strip('<>/')
            new_list.append(new_value)
        #print new_list
            
        data_list = []
        for another_value in new_list:
            a_new_value = another_value.split("=")
            if len(a_new_value) == 2:
                #print a_new_value
                data_list.append(a_new_value)
        #print data_list

        data_list_index = 0

        for final_data_line in data_list:
            # print final_data_line
            if final_data_line[0] == 'measure':
                the_key = final_data_line[1].lower()  # for some reason gdata libs only like lowercase keys
                the_value = data_list[data_list_index + 3][1]
                #print the_key + "\t" + the_value
                data_dictionary[the_key.strip('"')] = the_value.strip('"')
                print the_key.strip('"') + " " + data_dictionary[the_key.strip('"')]
                key_list.append(the_key.strip('"'))
            data_list_index = data_list_index + 1

    print key_list

    data_sample = data_dictionary['date'] + " " + data_dictionary['time']
    for a_key in key_list:
        data_sample = data_sample + " " + data_dictionary[a_key]
        
    data_sample = data_sample + "\r\n"
    xw6048_datalog_fd.write(data_sample)

    print data_dictionary

    # write to google spreadsheet "inv_data"
    while True:
        try:
            entry = client.InsertRow(data_dictionary, key='asfjaegbaw24524aqfaanotherboguskey234fsd')  # get this key from the google spreadsheet's url
                                                                       # everything after key= and before &hl=en
            break
        except socket.error:
            time.sleep(10)
        except gdata.service.RequestError:
            time.sleep(60)
            client.ProgrammaticLogin()

    if isinstance(entry, gdata.spreadsheet.SpreadsheetsList):
      print "Insert row succeeded."
    else:
      print "Insert row failed."  

    return 0
    ## end of xw6048_data()

def mppt60_data(mppt60_datalog_fd, device_num):
    theurl = 'http://192.168.1.153/xml/MPPT60%20' + str(device_num) + '/rb.xml'
    # file = urllib2.urlopen(theurl)
    while True:
        try:
            file = urllib2.urlopen(theurl)
            break
        except urllib2.HTTPError:
            time.sleep(10)
    ### IF AT THIS POINT YOU A "httplib.BadStatusLine: HTTP/1.0 200Document follows"
    ### EXCEPTION GETS THROWN, YOU WILL HAVE TO CHANGE THE FOLLOWING IN THE 
    ### c:\Python27\lib\httplib.py CODE AFTER LINE 377 OF THE _read_status()
    ### FUNCTION DEFINITION.  INSERT THE FOLLOWING TWO LINES AFTER LINE 377
    ### OF THAT httplib.py CODE:
    ###
    ###    if status == "200Document":
    ###       status = "200"
    ###
    ### REASON WHY:  WHOEVER WROTE THE WEBSERVER FOR THIS SYSTEM MADE A TYPO
    ###    THEY DID NOT HAVE A SPACE BETWEEN 200 AND DOCUMENT FOR THE STATUS
    ###    RESPONSE TO THE HTTP GET REQUEST.
    data = file.read()
    file.close()
    print data
    dom = parseString(data)
    PVInputxmlTag = dom.getElementsByTagName('group')[0].toxml()
    MPPTOutputxmlTag = dom.getElementsByTagName('group')[1].toxml()

    # just putting them into a single list that I can iterate through
    data_list = []
    data_list.append(PVInputxmlTag.encode("ascii"))
    data_list.append(MPPTOutputxmlTag.encode("ascii"))

    #print data_list

    data_dictionary = {}

    data_dictionary['date'] = time.strftime('%m/%d/%Y')
    data_dictionary['time'] = time.strftime("%H:%M")  

    key_list = []

    for data_line in data_list:
        split_data = data_line.split()
        #print data_line
        new_list = []
        for value in split_data:
            new_value = value.strip('<>/')
            new_list.append(new_value)
        #print new_list
            
        data_list = []
        for another_value in new_list:
            a_new_value = another_value.split("=")
            if len(a_new_value) == 2:
                #print a_new_value
                data_list.append(a_new_value)
        #print data_list

        data_list_index = 0

        for final_data_line in data_list:
            # print final_data_line
            if final_data_line[0] == 'measure':
                the_key = final_data_line[1].lower()  # for some reason gdata libs only like lowercase keys
                the_value = data_list[data_list_index + 3][1]
                #print the_key + "\t" + the_value
                data_dictionary[the_key.strip('"')] = the_value.strip('"')
                print the_key.strip('"') + " " + data_dictionary[the_key.strip('"')]
                key_list.append(the_key.strip('"'))
            data_list_index = data_list_index + 1

    data_sample = data_dictionary['date'] + " " + data_dictionary['time']
    for a_key in key_list:
        data_sample = data_sample + " " + data_dictionary[a_key]
    data_sample = data_sample + "\r\n"
    mppt60_datalog_fd.write(data_sample)

    # write to google spreadsheet "mppt_x"  (x is either 0 or 1)
    while True:
        try:
            if device_num == 0:  # key for mppt_0
                entry = client.InsertRow(data_dictionary, key='aerfaqiejfaf423415yepanotherboguskey4145sdffa')  # get this key from the google spreadsheet's url
                                                                       # everything after key= and before &hl=en
            elif device_num == 1:  # key for mppt_1
                entry = client.InsertRow(data_dictionary, key='45tafkjar43yetanotherboguskeyq344tgartq454234')  # get this key from the google spreadsheet's url
                                                                       # everything after key= and before &hl=en
            break
        except socket.error:
            time.sleep(10)
        except gdata.service.RequestError:
            time.sleep(60)
            client.ProgrammaticLogin()

    if isinstance(entry, gdata.spreadsheet.SpreadsheetsList):
      print "Insert row succeeded."
    else:
      print "Insert row failed."  

    return 0

    ## end of mppt60_data():

def xwags_data(xwags_datalog_fd):
    # file = urllib2.urlopen('http://192.168.1.153/xml/XW%20AGS%200/rb.xml')
    while True:
        try:
            file = urllib2.urlopen('http://192.168.1.153/xml/XW%20AGS%200/rb.xml')
            break
        except urllib2.HTTPError:
            time.sleep(10)   
    ### IF AT THIS POINT YOU A "httplib.BadStatusLine: HTTP/1.0 200Document follows"
    ### EXCEPTION GETS THROWN, YOU WILL HAVE TO CHANGE THE FOLLOWING IN THE 
    ### c:\Python27\lib\httplib.py CODE AFTER LINE 377 OF THE _read_status()
    ### FUNCTION DEFINITION.  INSERT THE FOLLOWING TWO LINES AFTER LINE 377
    ### OF THAT httplib.py CODE:
    ###
    ###    if status == "200Document":
    ###       status = "200"
    ###
    ### REASON WHY:  WHOEVER WROTE THE WEBSERVER FOR THIS SYSTEM MADE A TYPO
    ###    THEY DID NOT HAVE A SPACE BETWEEN 200 AND DOCUMENT FOR THE STATUS
    ###    RESPONSE TO THE HTTP GET REQUEST.
    
    data = file.read()
    file.close()
    print data
    dom = parseString(data)
    GeneratorOperationxmlTag = dom.getElementsByTagName('group')[0].toxml()

    #print GeneratorOperationxmlTag

    data_line = GeneratorOperationxmlTag.encode("ascii")
    #print data_list

    data_dictionary = {}

    data_dictionary['date'] = time.strftime('%m/%d/%Y')
    data_dictionary['time'] = time.strftime("%H:%M")  

    split_data = data_line.split()

    new_list = []
    for value in split_data:
        new_value = value.strip('<>/')
        new_list.append(new_value)

    #print new_list
    
    data_list = []
    for another_value in new_list:
        a_new_value = another_value.split("=")
        if len(a_new_value) == 2:
            #print a_new_value
            data_list.append(a_new_value)    

        data_list_index = 0

    for final_data_line in data_list:
        # print final_data_line
        if final_data_line[0] == 'measure':
            the_key = final_data_line[1].lower()  # for some reason gdata libs only like lowercase keys
            the_value = data_list[data_list_index + 1][1]
            #print the_key + "\t" + the_value
            data_dictionary[the_key.strip('"')] = the_value.strip('"')
            print the_key.strip('"') + " " + data_dictionary[the_key.strip('"')]
        data_list_index = data_list_index + 1
        
    data_sample = data_dictionary['date'] + " " + data_dictionary['time'] + " " + data_dictionary['genopstate'] + "\r\n"

    xwags_datalog_fd.write(data_sample)

    # write to google spreadsheet "ags_data"
    while True:
        try:
            entry = client.InsertRow(data_dictionary, key='1324514tfyougotitanotherboguskey324afaearfasdf')  # get this key from the google spreadsheet's url
                                                                       # everything after key= and before &hl=en
            break
        except socket.error:
            time.sleep(10)
        except gdata.service.RequestError:
            time.sleep(60)
            client.ProgrammaticLogin()

    if isinstance(entry, gdata.spreadsheet.SpreadsheetsList):
      print "Insert row succeeded."
    else:
      print "Insert row failed."  
    

    return 0
    ## end of xwags_data():

    

#### MAIN #####

while True:
    powersystem_data(system_datalog_fd)
    xw6048_data(xw6048_datalog_fd)
    mppt60_data(mppt60_0_datalog_fd, 0)
    mppt60_data(mppt60_1_datalog_fd, 1)
    xwags_data(xwags_datalog_fd)
    time.sleep(60)

system_datalog_fd.close()
xw6048_datalog_fd.close()
mppt60_0_datalog_fd.close()
mppt60_1_datalog_fd.close()
xwags_datalog_fd.close()




        




    
    
    




